package com.audaque.cloud.iqs;

import com.audaque.cloud.iqs.utils.KerberosUtil;
import com.audaque.cloud.iqs.utils.LoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Properties;


/**
 * 登录hive
 * @author: 罗勇
 * @date: 2021-12-31 17:08
 */
@Slf4j
public abstract class HiveBuilder {

	public static String HIVE_AUTH = "KERBEROS";

	public ZkHiveBean buildRmsHive(Map<String, String> customMap,Long tenantId) throws IOException {
		ZkHiveBean zkHiveBean = buildZkHiveBean(customMap,tenantId);
		if (HIVE_AUTH.equalsIgnoreCase(zkHiveBean.getAuth())) {
			String zookeeperDefaultServerPrincipal = "zookeeper/" + KerberosUtil.getUserRealm();
			LoginUtil.setJaasConf("Client", getUserName(customMap), zkHiveBean.getKeytabFile().getAbsolutePath());
			LoginUtil.setZookeeperServerPrincipal("zookeeper.server.principal", zookeeperDefaultServerPrincipal);
			LoginUtil.login(getUserName(customMap), zkHiveBean.getKeytabFile().getAbsolutePath(), zkHiveBean.getKrb5confFile().getAbsolutePath(), new Configuration());
		}
		LoginUtil.processZkSsl(zkHiveBean.getClientInfo());
		return zkHiveBean;
	}

	private ZkHiveBean buildZkHiveBean(Map<String, String> customMap,Long tenantId) throws IOException {
		ZkHiveBean zkHiveBean = new ZkHiveBean();
		String tempDir = System.getProperty("java.io.tmpdir") + File.separator;
		File hiveClientFile = new File(tempDir + customMap.get("hiveclient") + ".properties");
		if (!hiveClientFile.exists()) {
			downloadFile(customMap.get("hiveclient"), hiveClientFile,tenantId);
		}
		zkHiveBean.setHiveClientFile(hiveClientFile);
		File krb5confFile = new File(tempDir + customMap.get("krb5conf") + ".conf");
		if (!krb5confFile.exists()) {
			downloadFile(customMap.get("krb5conf"), krb5confFile,tenantId);
		}
		zkHiveBean.setKrb5confFile(krb5confFile);
		File keytabFile = new File(tempDir + customMap.get("keytab") + ".keytab");
		if (!keytabFile.exists()) {
			downloadFile(customMap.get("keytab"), keytabFile,tenantId);
		}
		zkHiveBean.setKeytabFile(keytabFile);
		Properties clientInfo = new Properties();
		try (FileInputStream inputStream = FileUtils.openInputStream(hiveClientFile)) {
			clientInfo.load(inputStream);
		}
		zkHiveBean.setZkQuorum(clientInfo.getProperty("zk.quorum"));
		zkHiveBean.setAuth(clientInfo.getProperty("auth"));
		zkHiveBean.setSaslQop(clientInfo.getProperty("sasl.qop"));
		zkHiveBean.setZooKeeperNamespace(clientInfo.getProperty("zooKeeperNamespace"));
		zkHiveBean.setServiceDiscoveryMode(clientInfo.getProperty("serviceDiscoveryMode"));
		zkHiveBean.setPrincipal(clientInfo.getProperty("principal"));
		zkHiveBean.setAuditAddition(clientInfo.getProperty("auditAddition"));
		System.setProperty("java.security.krb5.conf", krb5confFile.getAbsolutePath());
		String username = getUserName(customMap);
		String driver = getDriverClass(customMap);
		zkHiveBean.setUserName(username);
		zkHiveBean.setDriverClass(driver);
		zkHiveBean.setClientInfo(clientInfo);
		zkHiveBean.buildUrl();
		return zkHiveBean;
	}

	protected abstract String getDriverClass(Map<String, String> customMap);


	public abstract void downloadFile(String fileId, File downloadFile,Long tenantId);

	public abstract String getUserName(Map<String, String> customMap);
}
